package com.zhou.yygh.hosp.controller.api;

import com.baomidou.mybatisplus.extension.api.R;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.zhou.yygh.common.exception.YyghException;
import com.zhou.yygh.common.helper.HttpRequestHelper;
import com.zhou.yygh.common.result.Result;
import com.zhou.yygh.common.result.ResultCodeEnum;
import com.zhou.yygh.common.utills.MD5;
import com.zhou.yygh.hosp.service.DepartmentService;
import com.zhou.yygh.hosp.service.HospitalService;
import com.zhou.yygh.hosp.service.HospitalSetService;
import com.zhou.yygh.hosp.service.ScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zhou.hospital.model.hosp.Department;
import zhou.hospital.model.hosp.Hospital;
import zhou.hospital.model.hosp.Schedule;
import zhou.hospital.vo.hosp.DepartmentQueryVo;
import zhou.hospital.vo.hosp.DepartmentVo;
import zhou.hospital.vo.hosp.ScheduleQueryVo;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
@Api(tags = "部门设置管理")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    //上传医院信息
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {

        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);


        //得到密钥
        String sign = (String) paramMap.get("sign");

        //通过查询数据库得到密钥
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //MD5加密后比较两者的key是否相等
        String encrypt = MD5.encrypt(signKey);
        if (!sign.equals(encrypt)) {
            System.out.println(!sign.equals(encrypt));
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }


        //传输过程中图片地址" + " 会被替换成 " " 转义回来
        String logoData = (String)paramMap.get("logoData");
        if (!StringUtils.isEmpty(logoData)) {
            logoData = logoData.replaceAll(" ","+");
            paramMap.put("logoData",logoData);

        }

        hospitalService.save(paramMap);
        return Result.ok();

    }

    //查询医院信息
    @PostMapping("/hospital/show")
    public Result showHospital(HttpServletRequest request) {
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);

        //得到密钥
        String sign = (String) paramMap.get("sign");

        //通过查询数据库得到密钥
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //MD5加密后比较两者的key是否相等
        String encrypt = MD5.encrypt(signKey);
        if (!sign.equals(encrypt)) {
            System.out.println(!sign.equals(encrypt));
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }


        Hospital hospital = hospitalService.getHospital(hoscode);
        return Result.ok(hospital);
    }

    //移除部门
    @PostMapping("/department/remove")
    public Result removerDept(HttpServletRequest request) {
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        //部门编号
        String depcode = (String) paramMap.get("depcode");
        //得到密钥
        String sign = (String) paramMap.get("sign");

        //通过查询数据库得到密钥
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //MD5加密后比较两者的key是否相等
        String encrypt = MD5.encrypt(signKey);
        if (!sign.equals(encrypt)) {
            System.out.println(!sign.equals(encrypt));
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.deleteDept(hoscode, depcode);
        return Result.ok();
    }

    //获得所有部门
    @PostMapping("/department/list")
    public Result getDept(HttpServletRequest request) {
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);

        String hoscode = (String) paramMap.get("hoscode");

        int page = StringUtils.isEmpty(paramMap.get("page")) ?
                1 : Integer.parseInt((String) (paramMap.get("page")));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ?
                1 : Integer.parseInt((String) (paramMap.get("limit")));

        //得到密钥
        String sign = (String) paramMap.get("sign");
        //通过查询数据库得到密钥
        String signKey = hospitalSetService.getSignKey(hoscode);
        //MD5加密后比较两者的key是否相等
        String encrypt = MD5.encrypt(signKey);
        if (!sign.equals(encrypt)) {
            System.out.println(!sign.equals(encrypt));
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> deparment = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(deparment);
    }

    //上传部门信息
    @PostMapping("/saveDepartment")
    public Result saveDept(HttpServletRequest request) {
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        //得到密钥
        String sign = (String) paramMap.get("sign");

        //通过查询数据库得到密钥
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //MD5加密后比较两者的key是否相等
        String encrypt = MD5.encrypt(signKey);
        if (!sign.equals(encrypt)) {
            System.out.println(!sign.equals(encrypt));
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.save(paramMap);
        return Result.ok();
    }

    //上传排班
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        //得到密钥
        String sign = (String) paramMap.get("sign");

        //通过查询数据库得到密钥
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //MD5加密后比较两者的key是否相等
        String encrypt = MD5.encrypt(signKey);
        if (!sign.equals(encrypt)) {
            System.out.println(!sign.equals(encrypt));
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }

    //查询排班
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request) {

        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        //科室编号
        String  depcode =(String) paramMap.get("depcode");
        //得到密钥
        String sign = (String) paramMap.get("sign");

        //通过查询数据库得到密钥
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //MD5加密后比较两者的key是否相等
        String encrypt = MD5.encrypt(signKey);
        if (!sign.equals(encrypt)) {
            System.out.println(!sign.equals(encrypt));
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        int page = StringUtils.isEmpty(paramMap.get("page")) ?
                1 : Integer.parseInt((String) (paramMap.get("page")));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ?
                1 : Integer.parseInt((String) (paramMap.get("limit")));


        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        Page<Schedule> all = scheduleService.findSchedule(page, limit, scheduleQueryVo);
        return Result.ok(all);
    }

    //删除排班
    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request){

        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        //科室编号
        String  hosScheduleId =(String) paramMap.get("hosScheduleId");

        //得到密钥
        String sign = (String) paramMap.get("sign");

        //通过查询数据库得到密钥
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //MD5加密后比较两者的key是否相等
        String encrypt = MD5.encrypt(signKey);
        if (!sign.equals(encrypt)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.romoveScheduleId(hoscode,hosScheduleId);
       return Result.ok();
    }



}
